package pacs_server

import (
	"fmt"
	nested "github.com/antonfisher/nested-logrus-formatter"
	"github.com/sirupsen/logrus"
)

const LOCKFILE = "_locked"

func (pi *PacsServer) GetStudiesIds() (ids []string) {
	data, err := pi.DbStudiesHandler().Fields("studies_id").Get()
	if err != nil {
		return nil
	} else {
		for _, v := range data {
			val, ok := v["studies_id"]
			if ok {
				ids = append(ids, val.(string))
			}
		}
		return ids
	}
}

func (pi *PacsServer) FuzzyFindStudiesId(studiesId string) (cnt int, studiesIds []string, err error) {
	return pi.FuzzyFindStudiesCustomField("studies_id", studiesId)
}
func (pi *PacsServer) FuzzyFindPatientId(pid string) (cnt int, studiesIds []string, err error) {
	return pi.FuzzyFindStudiesCustomField("patient_id", pid)
}
func (pi *PacsServer) FuzzyFindPatientName(pn string) (cnt int, studiesIds []string, err error) {
	return pi.FuzzyFindStudiesCustomField("patient_name", pn)
}
func (pi *PacsServer) FuzzyFindStudiesCustomField(field, keyword string) (cnt int, studiesIds []string, err error) {
	selector := fmt.Sprintf("%%%s%%", keyword)

	i, err := pi.DbStudiesHandler().Where(field, "like", selector).Count()
	if i < 1 {
		return 0, nil, err
	}

	data, err := pi.DbStudiesHandler().Where(field, "like", selector).Fields("studies_id").Get()
	if err != nil {
		return 0, nil, err
	}

	for _, v := range data {
		studiesIds = append(studiesIds, v[field].(string))
	}
	return int(i), studiesIds, nil
}

func (pi *PacsServer) FuzzyFindSeriesById(seriesId string) (cnt int, seriesIds []string, err error) {
	return pi.FuzzyFindSeriesCustomField("series_id", seriesId)
}
func (pi *PacsServer) FuzzyFindSeriesCustomField(field, keyword string) (cnt int, seriesIds []string, err error) {
	selector := fmt.Sprintf("%%%s%%", keyword)

	i, err := pi.DbSeriesHandler().Where(field, "like", selector).Count()
	if i < 1 {
		return 0, nil, err
	}

	data, err := pi.DbStudiesHandler().Where(field, "like", selector).Fields("studies_id").Get()
	if err != nil {
		return 0, nil, err
	}

	for _, v := range data {
		seriesIds = append(seriesIds, v[field].(string))
	}
	return int(i), seriesIds, nil
}

func (pi *PacsServer) FuzzyFindInstanceById(instanceId string) (cnt int, instanceIds []string, err error) {
	return pi.FuzzyFindInstanceCustomField("instance_id", instanceId)
}
func (pi *PacsServer) FuzzyFindInstanceCustomField(field, keyword string) (cnt int, instanceIds []string, err error) {
	selector := fmt.Sprintf("%%%s%%", keyword)

	i, err := pi.DbSeriesHandler().Where(field, "like", selector).Count()
	if i < 1 {
		return 0, nil, err
	}

	data, err := pi.DbInstanceHandler().Where(field, "like", selector).Fields("instance_id").Get()
	if err != nil {
		return 0, nil, err
	}

	for _, v := range data {
		instanceIds = append(instanceIds, v[field].(string))
	}
	return int(i), instanceIds, nil
}

func (pi *PacsServer) Init(prefix string, enableLog bool) {
	pi.ID = prefix
	if enableLog {
		logger := logrus.New()
		logger.SetFormatter(&nested.Formatter{
			FieldsOrder:     []string{"module", "msg"},
			TimestampFormat: "20060102T150405",
			HideKeys:        true,
			NoColors:        true,
		})
		logger.SetLevel(logrus.TraceLevel)
		pi.logger = logger.WithField("module", prefix)
		pi.Log("bridge show log")
	}
}
func (pi *PacsServer) Log(msg ...interface{}) {
	if pi.logger != nil {
		pi.logger.Print(msg...)
	}
}
func (pi *PacsServer) Logf(format string, msg ...interface{}) {
	if pi.logger != nil {
		pi.logger.Printf(format, msg...)
	}
}
